package cn.com.dhcc.turbo.dao.registry;

import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.ModuleFeatureBeanRowWapper;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.turbo.entity.registry.AdapterBase;

/**
 * 适配器基础信息DAO
 * 
 * @日期 2015-4-20 上午10:30:34
 * @作者  高强
 */
@Repository
public class AdapterBaseDao extends AbstractCrudDao<AdapterBase> {
//	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);
	
	/**
	 * 获取适配器编码最大信息
	 * @return
	 * @throws DaoException
	 */
	public long localMaxCode(String adapterType,String orgCode) throws DaoException {
		try {
			WhereCondition condition = new WhereCondition();
			condition.where().eq("adapter_type", adapterType);
			if("1".equals(adapterType)){
				condition.and().eq("org_code", orgCode);
			}
			final String sql = "select max(adapter_code) from " + SI_TABLE("turbo_adapter_base");
			long maxCode =(CM.getDao().queryForLong("获取最大节点编码", sql,condition, new Object[]{}) != null ? CM.getDao().queryForLong("获取最大节点编码", sql,condition, new Object[]{}):0)+1;
			return  maxCode;
		} catch (DaoException e) {
			throw new DaoException("获取适配器最大编码失败！", e);
		}
	}
	
	/**
	 * 获取适配器列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws DaoException
	 */
	public PagerData<AdapterBase> getAdapterBaseList(Pager pager,WhereCondition condition,String orgCode)  throws DaoException {
		String sql = " select t.*,case when t.org_code = '"+orgCode+"' and t.adapter_type=1 then 1 else  0 end edit from " + SI_TABLE("turbo_adapter_base") +" t";
		return CM.getDao().getPagerList("获取适配器列表", sql, new ModuleFeatureBeanRowWapper<AdapterBase>(AdapterBase.class), pager,condition,new Object[]{});
	}
	
}
